--
-- Author: shunguo.chen
-- Date: 2017-6-28 17:42:49
--
-- SettingBattlePanel

local TabsBtn = require("app.widget.TabsBtn")

local ClassRef = app.mvc.createPanelClass(...)

function ClassRef.create(node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.SettingBattlePanel))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)
	local setting = UD.setting

	-- init btns
	for i = 1, 10 do
		local btn = self._ui["Panel/Panel_" .. i .. "/selected_btn"]
		btn:setTag(i)
		btn:addClickEventListener(handler(self, self.onBtnClick))
		if i == 1 then
			btn:setBright(not setting:isShowBattleHead())
		elseif i == 2 then
			btn:setBright(not setting:isShowBattlePalHp())
		elseif i == 3 then
			btn:setBright(not setting:isShowBattlePalName())
		elseif i == 4 then
			btn:setBright(not setting:isShowBattleOutline())
		elseif i == 5 then
			btn:setBright(not setting:isShowBattleFoeHp())
		elseif i == 6 then
			btn:setBright(not setting:isShowBattleFoeName())
		elseif i == 7 then
			btn:setBright(not setting:isShowBattleTOT())
		elseif i == 8 then
			btn:setBright(not setting:isShowBattleFrog())
		elseif i == 9 then
			btn:setBright(not setting:isShowBattleChat())
		elseif i == 10 then
			btn:setBright(not setting:isShowDBM())
		end
	end

	local btnResetBattleTalkContent = self._ui["Panel/Panel_bom/btn_recover"]
	btnResetBattleTalkContent:addClickEventListener(handler(self, self.onBtnClickResetContent))

	for i = 1, 4 do
		local strContent = setting:getBattleTalkContent(i)
		local editBox = display.replaceTextFieldWithEditBox(self._ui["Panel/Panel_bom/speak_"..i.."/TextField"])
		editBox:setTag(i)
		editBox:setText(strContent)
		editBox:registerScriptEditBoxHandler(function(strEventName,sender)
			if strEventName == "changed" then

			elseif strEventName == "return" or strEventName == "ended" then
				local content = sender:getText()
				if string.ulen(content) > Constant.BattleTalkShowEditNum then
					content = string.usubstr(content,Constant.BattleTalkShowEditNum)
				end
				ClassRef.setBattleText(editBox, content)
			end
		end)
		self._ui["Panel/Panel_bom/speak_"..i.."/TextField"] = editBox

		local btn = self._ui["Panel/Panel_bom/speak_"..i.."/Button"]
		btn:setTag(i)
		btn:addClickEventListener(handler(self, self.onBtnClickEditContent))
	end
end

function ClassRef:onBtnClick(sender)
	local tag = sender:getTag()
	local isBright = sender:isBright()
	sender:setBright(not isBright)
	local setting = UD.setting
	if tag == 1 then
		setting:setShowBattleHead(isBright)
		EM:notify("onSettingShowBattleHead",isBright)
	elseif tag == 2 then
		setting:setShowBattlePalHp(isBright)
		EM:notify("onSettingShowBattlePalHp",isBright)
	elseif tag == 3 then
		setting:setShowBattlePalName(isBright)
		EM:notify("onSettingShowBattlePalName",isBright)
	elseif tag == 4 then
		setting:setShowBattleOutline(isBright)
		EM:notify("onSettingShowBattleOutline",isBright)
	elseif tag == 5 then
		setting:setShowBattleFoeHp(isBright)
		EM:notify("onSettingShowBattleFoeHp",isBright)
	elseif tag == 6 then
		setting:setShowBattleFoeName(isBright)
		EM:notify("onSettingShowBattleFoeName",isBright)
	elseif tag == 7 then
		setting:setShowBattleTOT(isBright)
		EM:notify("onSettingShowBattleTOT",isBright)
	elseif tag == 8 then
		setting:setShowBattleFrog(isBright)
		EM:notify("onSettingShowBattleFrog",isBright)
	elseif tag == 9 then
		setting:setShowBattleChat(isBright)
		EM:notify("onSettingShowBattleChat",isBright)
	elseif tag == 10 then
		setting:setShowDBM(isBright)
	end
	UD:updateSetting(setting)
end

function ClassRef:onBtnClickResetContent(sender)
	local confirmDialog = display.newConfirmDialog(L("tips_resetBattleCry"),
		function()
			local setting = UD.setting
			for i = 1, 4 do
				local strContent = L(GD:getSystemStrPar("defaultBattleCry"..i, ""))
				setting:setBattleTalkContent(i, strContent)
				self._ui["Panel/Panel_bom/speak_"..i.."/TextField"]:setText(strContent)
			end
			UD:updateSetting(setting)
		end)
	UIHandler:addPopupUi(confirmDialog)
end

function ClassRef:onBtnClickEditContent(sender)
	local tag = sender:getTag()
	local editBox = self._ui["Panel/Panel_bom/speak_"..tag.."/TextField"]
	if editBox then
		editBox:touchDownAction(editBox, 2)
	end
end

function ClassRef.setBattleText(sender, content)
	local function checkCB(contentFinal)
		if not cc.isDead(sender) then
			local setting = UD.setting
			setting:setBattleTalkContent(sender:getTag(), contentFinal)
			UD:updateSetting(setting)
			sender:setText(contentFinal)
		end
	end
	if app:isNeedCheckInput() then
		app:checkInput(content, function(response,errorMsg,code)
			if code == 200 then
				if response and response.result then
					content = response.result
					checkCB(content)
				end
			else
				display.pushToast(L("network_error"))
			end
		end, true)
	else
		checkCB(content)
	end
end

return ClassRef